<template>
	<view class="content">
		<view class="nav">
			<u-dropdown active-color="#0FC6C2" activeColor="#0FC6C2" duration="1">
				<u-dropdown-item v-model="type" :value="type" title="任务" :options="typeList"
					@change="changeType"></u-dropdown-item>
				<u-dropdown-item v-model="status" title="状态" :value="status" @change="changeStatus"
					:options="statusList"></u-dropdown-item>
			</u-dropdown>
		</view>

		<z-paging default-page-no="1" :auto="false" default-page-size="10" :fixed="false"
			:paging-style="{height:'80vh'}" ref="paging" v-model="tdList" @query="queryList">
			<view>
				<table border-color="transparent" class="table">
					<thead>
						<tr class="t_header">
							<th class="t_header_th" v-for="item in thList" :key="item.id">{{item.name}}</th>
						</tr>
					</thead>
					<tbody>
						<tr class="t_body" v-for="item in tdList" :key="item.id" @click="handleToDetail(item)">
							<td class="table_td">{{item.rowNum}}</td>
							<td class="table_td">{{item.taskName}}</td>
							<td class="table_td">{{item.operatorDate}}</td>
							<td :class="item.status == '0' ? 'table_active' : 'table_td'">
								{{getCurrentTaskStatus(item.taskStatus)}}
							</td>
						</tr>
					</tbody>
				</table>
			</view>
		</z-paging>
		<view class="footer_view">
			<ym-footer></ym-footer>
		</view>
	</view>

</template>

<script setup>
	import {
		ref,
		onMounted
	} from 'vue'
	import {
		queryDocTaskList,
		getProjectTypeDict,
		queryNotificationRed
	} from '@/apis/api.js'
	import {
		onLoad,
		onShow,
		onPageScroll,
		onReachBottom
	} from '@dcloudio/uni-app'
	// import {

	// } from '../../components/stateFooterView/stateFooterView.vue'


	const paging = ref(null)

	const type = ref('0')
	const status = ref(2)
	const pageNo = ref(1)
	const typeList = ref([{
		label: '全部',
		value: 1,
	}])
	const statusList = ref([{
			label: '全部',
			value: 2,
		},
		{
			label: '待执行',
			value: 0,
		},
		{
			label: '已结束',
			value: 1,
		},
	])
	const thList = ref([{
		name: '编号',
		id: 1
	}, {
		name: '任务名称',
		id: 2
	}, {

		name: '执行时间',
		id: 3
	}, {

		name: '状态',
		id: 4
	}, ])
	const tdList = ref([
	// 	{
	// 	rowNum: '3',
	// 	taskType: '0',
	// 	operatorDate: '2024-06-17 09:32',
	// 	taskStatus: '0',
	// 	projectId: '1',
	// 	docCode: '',
	// 	orgCode: '',
	// 	patientId: '',
	// 	followMainId: ''
	// },
	])
	const handleToDetail = (item) => {
		console.log(item.taskType)
		let taskT = getCurrentTaskWorkType(item.taskType)
		if (taskT == '筛查任务') {
			uni.navigateTo({
				url: '/pages/index/csDetail?projectId=' + item.projectId + '&docCode=' + item.docCode
			})
		} else if (taskT == '通知任务') {
			uni.navigateTo({
				url: '/pages/index/pqDetail?patientSiftId=' + item.patientSiftId + '&docCode=' + item.docCode +
					'&orgCode=' + item.orgCode
			})

		} else if (taskT == '随访任务') {
			uni.navigateTo({
				url: '/pages/index/sfDetail?followMainId=' + item.followMainId
			})
		}
	}
	// 选择类型
	const changeType = () => {
		console.log(type.value)
		paging.value.clear()
		queryCurrentDocTaskList(type.value, status.value, 1)
	}
	// 选择类型
	const changeStatus = () => {
		console.log(status.value)
		paging.value.clear()
		queryCurrentDocTaskList(type.value, status.value, 1)
	}

	onMounted(() => {
		const docUserInfo = uni.getStorageSync('userInfo')
		console.log('用户信息是啥' + docUserInfo)
		if (!docUserInfo) {
			console.log('获取用户信息报错')
			uni.reLaunch({
				url: '/pages/login/login'
			})
		}else {
			const userName = docUserInfo.docName
			console.log('1用户名字是' + userName)
			console.log('2用户名字是' + docUserInfo.docName)
			uni.setNavigationBarTitle({
				title: userName
			})
		}
	})
	onLoad(() => {
		getProjectDict()
	})
	
	onShow(() => {
		console.log("显示任务界面")
		getNotificationRedNetwork()
	})

	onReachBottom(() => {
		queryCurrentDocTaskList()
	})

	const getCurrentTaskWorkType = (type) => {
		var workType = ''
		workType = type == '0' ? '筛查任务' : (type == 1 ? '通知任务' : '随访任务')
		return workType
	}

	const getCurrentTaskStatus = (status) => {
		var taskStatus = ''
		taskStatus = status == '0' ? '待执行' : '已结束'
		return taskStatus
	}
	
	const getNotificationRedNetwork = async() => {
		try {
			const res = await queryNotificationRed()
			let isNews = res.data.isNews
			if (isNews == 1) {
				uni.showTabBarRedDot({
					index:1
				})
			}else {
				uni.hideTabBarRedDot({
					index:1
				})
			}
			console.log('当前通知的红点内容为' + res.data)
		}catch (error) {
			console.log('结果报错' + error.message)
		}
	}

	const getProjectDict = async () => {
		try {
			const res = await getProjectTypeDict()
			const resData = res.data
			console.log('结果' + JSON.stringify(res.data))
			typeList.value = []
			for (let i = 0; i < resData.length; i++) {
				const item = res.data[i]
				const newI = {
					label: item.name,
					value: item.id
				}
				console.log(newI)
				typeList.value[i] = newI
				console.log(typeList.value)
			}
			console.log('看看字典是否更行' + typeList.value)
			paging.value.clear()
			queryCurrentDocTaskList(type.value, status.value, 1)
		} catch (error) {
			console.log('结果报错' + error.message)
		}
	}

	const queryList = (pageNo, pageSize) => {
		queryCurrentDocTaskList(type.value, status.value, pageNo)
	}

	const queryCurrentDocTaskList = async (taskType, taskStatus, page) => {
		console.log('任务类型' + getCurrentTaskWorkType(taskType) + '任务状态' + getCurrentTaskStatus(taskStatus))
		const docUserInfo = uni.getStorageSync('userInfo')
		const docCode = docUserInfo.docCode
		var statusStr = taskStatus == '2' ? 'ALL' : taskStatus
		try {
			const res = await queryDocTaskList({
				taskStatus: statusStr,
				taskType: taskType,
				docCode: docCode,
				pageSize: 10,
				pageNo: page
			})
			console.log('获取到的任务信息是' + res.data)
			pageNo.value = page
			// if (page == 1) {
			// 	tdList.value = [...res.data.rows]
			// }else {
			// 	tdList.value = [...tdList.value, ...res.data]
			// }
			paging.value.complete(res.data.rows)
		} catch (err) {
			console.log(err.message)
			paging.value.complete(false)
		}
	}
</script>

<style scoped lang="scss">
	.nav {
		box-shadow: 0rpx 4rpx 8rpx 0rpx rgba(0, 0, 0, 0.04);
	}

	.table {
		border-bottom: none;
		border-radius: 16rpx;
		width: 96%;
		margin: 24rpx auto;
		font-size: 24rpx;

		.t_header {
			display: flex;
			// justify-content: space-around;
			height: 80rpx;
			align-items: center;
			background: #E5E6EB;
			font-weight: bold;

			.t_header_th:nth-child(1) {
				width: 20%;
				padding-left: 32rpx;
			}

			.t_header_th:nth-child(2) {
				width: 20%;
			}

			.t_header_th:nth-child(3) {
				width: 40%;
			}

			.t_header_th:nth-child(4) {
				width: 20%;
			}
		}

		.t_body {
			display: flex;
			height: 80rpx;
			align-items: center;
			border-bottom: 2rpx solid #E5E6EB;

			.table_td:nth-child(1) {
				width: 20%;
				padding-left: 32rpx;
			}

			.table_td:nth-child(2) {
				width: 20%;
			}

			.table_td:nth-child(3) {
				width: 40%;
			}

			.table_td:nth-child(4) {
				width: 20%;
			}

			.table_active {
				width: 20%;
				color: #0FC6C2;
			}
		}
	}

	.footer_view {
		position: absolute;
		bottom: 0;
		width: 100%;
	}
	
</style>